package com.ge.healthcare.autosc.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ge.healthcare.autosc.dao.intf.ProDiagsConnectivityDAO;
import com.ge.healthcare.autosc.dto.ScheduleBean;
import com.ge.healthcare.autosc.model.ActRequest;

/*
 *
 * Copyright (C) 2011 General Electric Company. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of the
 * General Electric Company (GE). You shall not disclose this software and
 * shall use it only in accordance with the terms of the license agreement
 * you entered into with GE.
 *
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE, OR NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES
 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING
 * THIS SOFTWARE OR ITS DERIVATIVES. 
 */

public class ProDiagsConnectivityDAOImpl implements ProDiagsConnectivityDAO {
	private final Logger logger = Logger.getLogger(ProDiagsConnectivityDAOImpl.class);
	@Autowired
	private HibernateTemplate hibernateTemplate;
	
	protected Class<ActRequest> getDataClass() {
		return ActRequest.class;
	}

	@Override
	public List<ActRequest> loadSchedule(ScheduleBean searchBean) {
		DetachedCriteria dc = DetachedCriteria.forClass(getDataClass());
		List<ActRequest> actRequestBean = new ArrayList<ActRequest>();
		try {
			if(searchBean.getTypeValue() != null){
				if(StringUtils.equals(searchBean.getType(), "systemid")){
					dc.add(Restrictions.eq("systemId", searchBean.getTypeValue()));
				} else if(StringUtils.equals(searchBean.getType(), "modeltype")){
					dc.add(Restrictions.eq("modelType", searchBean.getTypeValue()));
				}
			}
			
			actRequestBean = (List<ActRequest>)hibernateTemplate.findByCriteria(dc);
			System.out.println(actRequestBean.size());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
		}
		
		return actRequestBean;
	}
	
	public List<ActRequest> getRequestBySystemID(String systemId) {
		DetachedCriteria dc = DetachedCriteria.forClass(getDataClass());
		List<ActRequest> actRequestBean = new ArrayList<ActRequest>();
					dc.add(Restrictions.eq("systemId", systemId));
			
			actRequestBean = (List<ActRequest>)hibernateTemplate.findByCriteria(dc);
			System.out.println(actRequestBean.size());
		
		return actRequestBean;
	}
	
	public List<ActRequest> getRequestByModelType(String modelType){
		DetachedCriteria dc = DetachedCriteria.forClass(getDataClass());
		List<ActRequest> actRequestBean = new ArrayList<ActRequest>();
					dc.add(Restrictions.eq("modelType", modelType));
			
			actRequestBean = (List<ActRequest>)hibernateTemplate.findByCriteria(dc);
			System.out.println(actRequestBean.size());
		
		return actRequestBean;
	}
	
	public List<?> findByProperty(String propertyName, Object value) {
		String queryString = "from act_request where " + propertyName
				+ "= ?";
		return hibernateTemplate.find(queryString, value);
	}
	
	public static boolean isNullOrEmptyList(List<?> list) {
		if (isNullOrEmptyObject(list) || list.isEmpty()) {
			return true;
		}
		return false;
	}
	
	public static boolean isNullOrEmptyObject(Object input) {
		if (input == null) {
			return true;
		}
		return false;
	}

}
